package com.ll.base.dto;

import com.ll.base.util.ListUtil;
import lombok.Data;
import org.springframework.beans.BeanUtils;

import java.util.List;
import java.util.function.Function;

@Data
public class PageResultDTO<T> {
    private Integer pageNo;
    private Integer pageSize;
    private Long total;
    private List<T> datas;

    public static <T, R> PageResultDTO<R> PageBuildDTO(PageResultDTO<T> PageResultDTO, Function<T, R> function) {
        PageResultDTO<R> result = new PageResultDTO<R>();
        BeanUtils.copyProperties(PageResultDTO, result);
        result.setTotal(PageResultDTO.getTotal());
        result.setDatas(ListUtil.toList(PageResultDTO.getDatas(), function));
        return result;
    }

    public PageResultDTO(List<T> datas, Long total, PageReqDTO pageReq) {
        this.datas = datas;
        this.total = total;
        this.pageNo = pageReq.getPageNo();
        this.pageSize = pageReq.getPageSize();
    }

    public PageResultDTO(List<T> datas, Long total) {
        this.datas = datas;
        this.total = total;
    }

    public PageResultDTO(List<T> datas) {
        this.datas = datas;
    }

    public PageResultDTO() {
    }
}